package com.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Consumer;
import com.entity.ShopCart;
import com.google.gson.Gson;
import com.servers.ConsumerServers;
import com.servers.ShopCarService;

@Controller
public class ConsumerDemoController {
	@Autowired
	private ConsumerServers consumerServers;
	@Autowired
	private ShopCarService shopCarService;
	@RequestMapping("/goConsumerLogin")
	public ModelAndView goConsumerLogin(String urltest,String title,String buyNum ,HttpServletRequest request ) {
		ModelAndView moView=new ModelAndView();
		moView.setViewName("consumerlogin");
		moView.addObject("urltest", urltest);
		moView.addObject("title", title);
		moView.addObject("buyNum",buyNum );
		System.out.println(urltest+"@@@@@@@@@@@"+title+"@@@@@@@@"+buyNum);
		return moView;
	}
	@SuppressWarnings("unused")
	@ResponseBody
	@RequestMapping("/comlogin")
	@Test
	public String comlogin(String phonename, String pwdcom, HttpServletRequest request, HttpServletResponse response,
			String title, String buyNum, String urltest) throws UnsupportedEncodingException {
		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset="+"UTF-8");*/
		//System.out.println(phonename+"."+pwdcom+"@@@@@@@@aaaaaaaaaaaaaaaaaa@@@@@@@@@");
		//System.out.println(title+"@@@@@@@@@@@@@"+title+"@@@@@@@aaaaaaaaaaaaaaaaa@@@@@@@@@@"+urltest);
		Consumer userLoginDemo = consumerServers.userLoginDemo(phonename,pwdcom);
		Map<String, Object> map = new HashMap<>();
		Gson gson=new Gson();
		int ccid=userLoginDemo.getCid();
		System.out.println(userLoginDemo);
		System.out.println("@@@@@@@aaaaaaaaaaaaaaaaa@@@@@@@@@@"+urltest);
		//Consumer userLoginDemo = consumerServers.userLoginDemo("tom","123");
		if (userLoginDemo != null ) { 
			System.out.println("~~~~~~~~~~~~~~~~~用户存在 向session中存放数据~~~~~~~~~~~~~~~~~~");
			if (urltest.trim().equals("")) {
				List<ShopCart> findAllShopCarByCid = shopCarService.findAllShopCarByCid(ccid,1);
				// 登录成功后 将 name 用户名 保存在 session中
				
				request.getSession().setAttribute("consumer", userLoginDemo);
				request.getSession().setAttribute("CURR_COM", userLoginDemo.getCname());
				request.getSession().setAttribute("CURR_CID", userLoginDemo.getCid());
				request.getSession().setAttribute("CURR_NUM", findAllShopCarByCid.size());
				map.put("status", "3");
				//map.put("success", "http://10.50.5.73/Cake/");
				map.put("success", "commodity");
				return gson.toJson(map);
			}else {
				List<ShopCart> findAllShopCarByCid = shopCarService.findAllShopCarByCid(ccid,1);
				// 登录成功后 将 name 用户名 保存在 session中
				request.getSession().setAttribute("CURR_COM", userLoginDemo.getCname());
				request.getSession().setAttribute("CURR_CID", userLoginDemo.getCid());
				request.getSession().setAttribute("CURR_NUM", findAllShopCarByCid.size());
				
				/*request.setAttribute("title", title);
				request.setAttribute("buyNum", buyNum);
				request.setAttribute("urltest", urltest);*/
				
				//String[] split = urltest.split("/");
				//System.out.println(split[split.length - 1]+"~~~~~~~~~~~~~~~~~~~~~~~~~~"+urltest);
				map.put("status", "1");
				/*if (urltest==null) {
					map.put("urldemo", "http://10.50.5.73/Cake/commodity");
					System.out.println(gson.toJson(map)+"#######11111111111111111111111111#############");
				}else {
					System.out.println(gson.toJson(map)+"###########2222222222222222222222222#########");
				}*/
				map.put("urldemo", "details?name="+title);
				//System.out.println("@@@@@@@oooooooooooooooo@@@@@@@@@@"+urltest);
				System.out.println(gson.toJson(map)+"####################");
				return gson.toJson(map);
				
			}
		} else {
			System.out.println("~~~~~~~~~~~~~~~~~~~用户登录失败 ~~~~~~~~~~~~~~~~~~~");
			map.put("status", "2");
			map.put("error_COM", "用户密码错误,请重新输入");
			return gson.toJson(map);
		}

	}

	public ConsumerServers getConsumerServers() {
		return consumerServers;
	}
	
	public void setConsumerServers(ConsumerServers consumerServers) {
		this.consumerServers = consumerServers;
	}
	public ShopCarService getShopCarService() {
		return shopCarService;
	}
	public void setShopCarService(ShopCarService shopCarService) {
		this.shopCarService = shopCarService;
	}



}
